package com.smartbone.dao.daoImplemetation;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.context.ApplicationContext;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.smartbone.dao.daoInterface.ILookupDAO;
import com.smartbone.entity.Lookup;

/**
 * A data access object (DAO) providing persistence and search support for
 * Lookup entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.Lookup
 * @author MyEclipse Persistence Tools
 */

public class LookupDAO extends JpaDaoSupport implements ILookupDAO {
	// property constants
	public static final String GROUP_NO = "groupNo";
	public static final String ITEM_NO = "itemNo";
	public static final String DISPLAY_VALUE = "displayValue";
	public static final String DESCRIPT = "descript";
	public static final String STATUS = "status";

	/**
	 * Perform an initial save of a previously unsaved Lookup entity. All
	 * subsequent persist actions of this entity should use the #update()
	 * method. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#persist(Object)
	 * EntityManager#persist} operation.
	 * <p>
	 * User-managed Spring transaction example:
	 * 
	 * <pre>
	 * TransactionStatus txn = txManager
	 * 		.getTransaction(new DefaultTransactionDefinition());
	 * LookupDAO.save(entity);
	 * txManager.commit(txn);
	 * </pre>
	 * 
	 * @see <a href =
	 *      "http://www.myeclipseide.com/documentation/quickstarts/jpaspring#containermanaged">Spring
	 *      container-managed transaction examples</a>
	 * @param entity
	 *            Lookup entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(Lookup entity) {
		logger.info("saving Lookup instance");
		try {
			getJpaTemplate().persist(entity);
			logger.info("save successful");
		} catch (RuntimeException re) {
			logger.error("save failed", re);
			throw re;
		}
	}

	/**
	 * Delete a persistent Lookup entity. This operation must be performed
	 * within the a database transaction context for the entity's data to be
	 * permanently deleted from the persistence store, i.e., database. This
	 * method uses the {@link javax.persistence.EntityManager#remove(Object)
	 * EntityManager#delete} operation.
	 * <p>
	 * User-managed Spring transaction example:
	 * 
	 * <pre>
	 * TransactionStatus txn = txManager
	 * 		.getTransaction(new DefaultTransactionDefinition());
	 * LookupDAO.delete(entity);
	 * txManager.commit(txn);
	 * entity = null;
	 * </pre>
	 * 
	 * @see <a href =
	 *      "http://www.myeclipseide.com/documentation/quickstarts/jpaspring#containermanaged">Spring
	 *      container-managed transaction examples</a>
	 * @param entity
	 *            Lookup entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(Lookup entity) {
		logger.info("deleting Lookup instance");
		try {
			entity = getJpaTemplate()
					.getReference(Lookup.class, entity.getId());
			getJpaTemplate().remove(entity);
			logger.info("delete successful");
		} catch (RuntimeException re) {
			logger.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved Lookup entity and return it or a copy of it to
	 * the sender. A copy of the Lookup entity parameter is returned when the
	 * JPA persistence mechanism has not previously been tracking the updated
	 * entity. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
	 * operation.
	 * <p>
	 * User-managed Spring transaction example:
	 * 
	 * <pre>
	 * TransactionStatus txn = txManager
	 * 		.getTransaction(new DefaultTransactionDefinition());
	 * entity = LookupDAO.update(entity);
	 * txManager.commit(txn);
	 * </pre>
	 * 
	 * @see <a href =
	 *      "http://www.myeclipseide.com/documentation/quickstarts/jpaspring#containermanaged">Spring
	 *      container-managed transaction examples</a>
	 * @param entity
	 *            Lookup entity to update
	 * @return Lookup the persisted Lookup entity instance, may not be the same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public Lookup update(Lookup entity) {
		logger.info("updating Lookup instance");
		try {
			Lookup result = getJpaTemplate().merge(entity);
			logger.info("update successful");
			return result;
		} catch (RuntimeException re) {
			logger.error("update failed", re);
			throw re;
		}
	}

	public Lookup findById(Integer id) {
		logger.info("finding Lookup instance with id: " + id);
		try {
			Lookup instance = getJpaTemplate().find(Lookup.class, id);
			return instance;
		} catch (RuntimeException re) {
			logger.error("find failed", re);
			throw re;
		}
	}

	/**
	 * Find all Lookup entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the Lookup property to query
	 * @param value
	 *            the property value to match
	 * @return List<Lookup> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<Lookup> findByProperty(String propertyName, final Object value) {
		logger.info("finding Lookup instance with property: " + propertyName
				+ ", value: " + value);
		try {
			final String queryString = "select model from Lookup model where model."
					+ propertyName + "= :propertyValue";
			return getJpaTemplate().executeFind(new JpaCallback() {
				public Object doInJpa(EntityManager em)
						throws PersistenceException {
					Query query = em.createQuery(queryString);
					query.setParameter("propertyValue", value);
					return query.getResultList();
				}
			});
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Lookup> findByGroupNo(Object groupNo) {
		return findByProperty(GROUP_NO, groupNo);
	}

	public List<Lookup> findByItemNo(Object itemNo) {
		return findByProperty(ITEM_NO, itemNo);
	}

	public List<Lookup> findByDisplayValue(Object displayValue) {
		return findByProperty(DISPLAY_VALUE, displayValue);
	}

	public List<Lookup> findByDescript(Object descript) {
		return findByProperty(DESCRIPT, descript);
	}

	public List<Lookup> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	/**
	 * Find all Lookup entities.
	 * 
	 * @return List<Lookup> all Lookup entities
	 */
	@SuppressWarnings("unchecked")
	public List<Lookup> findAll() {
		logger.info("finding all Lookup instances");
		try {
			final String queryString = "select model from Lookup model";
			return getJpaTemplate().executeFind(new JpaCallback() {
				public Object doInJpa(EntityManager em)
						throws PersistenceException {
					Query query = em.createQuery(queryString);
					return query.getResultList();
				}
			});
		} catch (RuntimeException re) {
			logger.error("find all failed", re);
			throw re;
		}
	}

	public static ILookupDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ILookupDAO) ctx.getBean("LookupDAO");
	}
}